#include "sender.h"

Sender::Sender(MySocket &sock,string filename,string dirn) : clientSocket(sock)
{


    this->filename=filename;
    dir= dirn;
}

void Sender::send()
{

    ostringstream nom;
    unsigned int sendBytes=0;
    nom<<dir<<"/"<<filename;
    QFile file(nom.str().c_str());

    if ( file.open(QIODevice::ReadOnly) ) // ce test échoue si le fichier n'est pas ouvert
    {
        QDataStream in(&file);
          uint32_t size = file.size();

          clientSocket.write(&size,sizeof(uint32_t));
          char mess[BUFFER_SIZE+1];
          mess[BUFFER_SIZE] = '\0';
          uint32_t byteToSend = 0;
          while(sendBytes<size)
          {
              if(BUFFER_SIZE<=size-sendBytes)
                  byteToSend = BUFFER_SIZE;
              else
                  byteToSend=size-sendBytes;

              in.readRawData(mess, byteToSend);

              sendBytes += clientSocket.write(mess,byteToSend);
          }
     }
    else
    {
    }

}
